home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / defaultborder.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  5.2 KB  |  175 lines

  1. import java.awt.*;
  2. import java.util.Vector;
  3.  
  4. class DefaultBorder extends BufferedDrawer
  5. {
  6.     public DefaultBorder()
  7.     {
  8.         cornersImage = Util.loadImage("images/buttonCorners.gif", this);
  9.  
  10.         colors = new Vector();
  11.         colors.addElement(Util.GSBColor);    //Top and Left
  12.         colors.addElement(Util.GSBColor);    //Bottom and Right
  13.         colors.addElement(Util.GS2Color);    //Top and Left
  14.         colors.addElement(Util.GS8Color);    //Bottom and Right
  15.         colors.addElement(Util.GS5Color);    //Top and Left
  16.         colors.addElement(Util.GS5Color);    //Bottom and Right
  17.         
  18.         setLayout(null);
  19.     }
  20.  
  21.     /** 
  22.      * Returns the preferred size of this component.
  23.      * Overriden here to constrain height.
  24.      * @see java.awt.Component#getMinimumSize
  25.      * @see LayoutManager
  26.      */
  27.     public Dimension getPreferredSize()
  28.     {
  29.         Component components[] = getComponents();
  30.         Component comp;
  31.         Dimension s = null;
  32.         for (int i = 0; i < components.length; ++i)
  33.         {
  34.             comp = components[i];
  35.             if (comp instanceof java.awt.Button)
  36.             {
  37.                 Dimension compSize = comp.getPreferredSize();
  38.                 s = new Dimension(compSize.width + defaultBorderInsets.left + defaultBorderInsets.right, compSize.height + defaultBorderInsets.top + defaultBorderInsets.bottom);
  39.                 return s;
  40.             }
  41.         }
  42.         
  43.         if (s == null)
  44.             s = super.getPreferredSize();
  45.             
  46.         return s;
  47.     }
  48.  
  49.     /** 
  50.      * Sets the layout manager for this container.
  51.      * Overriden here to prevent setting the layout manager.
  52.      * @param mgr the specified layout manager
  53.      * @see #doLayout
  54.      * @see #getLayout
  55.      */
  56.     public void setLayout(LayoutManager mgr)
  57.     {
  58.         super.setLayout(null);
  59.     }
  60.  
  61.     /** 
  62.      * Does a layout on this Container.  Most programs should not call
  63.      * this directly, but should invoke validate instead.
  64.      * @see #setLayout
  65.      * @see #validate
  66.      */
  67.     public void doLayout()
  68.     {
  69.         Component components[] = getComponents();
  70.         Component comp;
  71.         for (int i = 0; i < components.length; ++i)
  72.         {
  73.             comp = components[i];
  74.             if (comp instanceof java.awt.Button)
  75.             {
  76.                 Dimension compSize = comp.getPreferredSize();
  77.                 comp.setBounds(defaultBorderInsets.top, defaultBorderInsets.left, compSize.width, compSize.height);
  78.                 return;
  79.             }
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * Renders the progress bar in an offscreen buffer.
  85.      * Renders the bar to indicate the current percent complete.
  86.      * @see #paint
  87.      * @see #setPercent
  88.      * @see #getPercent
  89.      */
  90.     protected void draw()
  91.     {
  92.         //Make sure we give the super a chance to do what it needs to.
  93.         super.draw();
  94.  
  95.         //Get the size of the ofscreen image.
  96.         int imageWidth    = workingImage.getWidth(this);
  97.         int imageHeight    = workingImage.getHeight(this);
  98.  
  99.         //Erase the contents.
  100.         workingGraphics.setColor(getBackground());
  101.         workingGraphics.fillRect(0, 0, imageWidth, imageHeight);
  102.  
  103.         if (cornersImage != null)
  104.             paintButtonBorder(this, workingGraphics, cornersImage, colors, new Point(0, 0), getSize());
  105.     }
  106.  
  107.     protected void paintButtonBorder(Component c, Graphics g, Image img, Vector colors, Point origin, Dimension size)
  108.     {
  109.         int        halfWidth    = img.getWidth(c) >> 1;
  110.         int        halfHeight    = img.getHeight(c) >> 1;
  111.         int        w            = size.width - 1;
  112.         int        h            = size.height - 1;
  113.         int        x            = origin.x;
  114.         int        y            = origin.y;
  115.  
  116.         // Draw the corners
  117.         paintButtonCorners(c, g, img, origin, size);
  118.         
  119.         Color oldColor = g.getColor();    
  120.         
  121.         //Draw the lines
  122.         for(int index = 0, count = 0; index < colors.size(); index += 2, ++count)
  123.         {
  124.             g.setColor((Color)colors.elementAt(index));
  125.             g.drawLine(halfWidth + x, count + y, w - halfWidth + x, count + y);                //Top
  126.             g.drawLine(count + x, halfHeight + y, count + x, h - halfHeight + y);            //Left
  127.             try
  128.             {
  129.                 g.setColor((Color)colors.elementAt(index + 1));
  130.             }
  131.             catch (ArrayIndexOutOfBoundsException exc) { }
  132.             g.drawLine(halfWidth + x, h - count + y, w - halfWidth + x, h - count + y);        //Bottom
  133.             g.drawLine(w - count + x, halfHeight + y, w - count + x, h - halfHeight + y);    //Right
  134.         }
  135.  
  136.         g.setColor(oldColor);
  137.     }
  138.  
  139.     protected void paintButtonCorners(Component c, Graphics g, Image img, Point origin, Dimension size)
  140.     {
  141.         int        imgWidth    = img.getWidth(c);
  142.         int        imgHeight    = img.getHeight(c);
  143.         int        halfWidth    = imgWidth >> 1;
  144.         int        halfHeight    = imgHeight >> 1;
  145.         int        x            = origin.x;
  146.         int        y            = origin.y;
  147.         
  148.         //Draw the Top Left corner
  149.         g.drawImage(img,
  150.           x, y, halfWidth + x, halfHeight + y,                                                            /*destination*/
  151.           0, 0, halfWidth, halfHeight,                                                                    /*source*/
  152.           c);
  153.         //Draw the Top Right corner
  154.         g.drawImage(img,
  155.           size.width - halfWidth + x, y, size.width + x, halfHeight + y,                                /*destination*/
  156.           halfWidth, 0, imgWidth, halfHeight,                                                            /*source*/
  157.           c);
  158.         //Draw the Bottom Left corner
  159.         g.drawImage(img,
  160.           x, size.height - halfHeight + y, halfWidth + x, size.height + y,                                /*destination*/
  161.           0, halfHeight, halfWidth, imgHeight,                                                            /*source*/
  162.           c);
  163.         //Draw the Bottom Right corner
  164.         g.drawImage(img,
  165.           size.width - halfWidth + x, size.height - halfHeight + y, size.width + x, size.height + y,    /*destination*/
  166.           halfWidth, halfHeight, imgWidth, imgHeight,                                                    /*source*/
  167.           c);
  168.     }
  169.  
  170.     protected final static Insets defaultBorderInsets = new Insets(3, 3, 3, 3);
  171.  
  172.     protected Image cornersImage;
  173.     protected Vector colors;
  174. }
  175.